package vn.com.epi.re.tools;

import java.io.IOException;

import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;

import vn.com.epi.re.table.ArticleTable;

public class Statistic {

    public static void loadCategory() throws IOException {
        HTable table = new HTable(ArticleTable.NAME);
        int[] cat = new int[1000];
        try {
            Scan scan = new Scan();
            scan.addColumn(ArticleTable.FAM_MAIN, ArticleTable.COL_CATEGORY_ID);        
            scan.addColumn(ArticleTable.FAM_MAIN, ArticleTable.COL_FIRST_VISIT_TIMESTAMPS);        
            ResultScanner rs = table.getScanner(scan);
            long current = System.currentTimeMillis() / 1000L;
            long oneWeek = 604800;
            for (Result r:rs) {
                long firstVisitedTimestamp = Bytes.toLong(r.getValue(ArticleTable.FAM_MAIN, ArticleTable.COL_FIRST_VISIT_TIMESTAMPS));
                if (current - firstVisitedTimestamp < oneWeek) {
                    int categoryId = Bytes.toInt(r.getValue(ArticleTable.FAM_MAIN, ArticleTable.COL_CATEGORY_ID));
                    cat[categoryId]++;
                }
            }
            for (int i = 0; i < 1000; i++)
                if (cat[i] > 0)
                    System.out.println(i + "        " + cat[i] + "    ");
        } catch (Exception e) {
            System.out.println("Error while load category: " + e.toString());
        } finally {
            TableUtils.closeQuietly(table);
        }                 
    }

    public static void main(String[] args) throws IOException {
        loadCategory();
    }
}
